/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.service.odonto;

import br.com.foxline.clinicplus.entity.Customer;
import br.com.foxline.clinicplus.entity.Diary;
import br.com.foxline.clinicplus.entity.odonto.Odontograma;
import br.com.foxline.clinicplus.enums.Reports;
import br.com.foxline.clinicplus.service.ClinicService;
import br.com.foxline.clinicplus.service.GenericService;
import br.com.foxline.clinicplus.service.ReportService;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author roniere
 */
@Stateless
public class OdontogramaService extends GenericService<Odontograma> {
    
    @EJB
    ClinicService clinicService;

    public OdontogramaService() {
        super(Odontograma.class);
    }
    
    public List<Odontograma> getOdontogramaPorPaciente(Customer customer){
        String sql = "Select o from Records r JOIN r.odontograma o JOIN r.customer c where c.id = :customer";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("customer", customer.getId());
        return query.getResultList();
    }
    
    public List<Odontograma> getPacientePorOdontograma(Odontograma o){
        String sql = "Select c from Records r JOIN r.odontograma o JOIN r.customer c where o.id = :odontograma";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("odontograma", o.getId());
        return query.getResultList();
    }
    
    
    public Diary getDiaryPorOdontograma(Odontograma o){
        String sql = "Select d from Diary d JOIN d.records r JOIN r.odontograma o  where o.id = :odontograma";
        Query query = getEntityManager().createQuery(sql).setMaxResults(1);
        query.setParameter("odontograma", o.getId());
        return (Diary) query.getSingleResult();
    }
    
    
    public void relatorioOdontograma(HashMap<String, Object> param, List<Odontograma> lst){
        
        URL subreport = Thread.currentThread().getContextClassLoader().getResource("br/com/foxline/clinicplus/reports/odontograma_dentes.jasper");

        param.put("SUBREPORT_DIR", subreport.toString().replace("odontograma_dentes.jasper" + "", "").replace("file:", ""));
        System.err.println(param.get("SUBREPORT_DIR"));
        param = clinicService.getCabecalho(param);
        ReportService.genReport(param, lst, Reports.ODONTOGRAMA);
    }

    
}
